// pages/search/search.js
Page({
  data: {
    key_word: "彭于晏",
    r_key_word: ["蔡徐坤", "陈立农", "范丞丞", "林彦俊", "朱正廷", "黄明昊", "时代少年团", "nine-percente"],
    flag: 0,
    allNoramalData: [{
        id: 1000,
        cover: "/image/sucai/物理老师打篮球一百个只进了三个_1_蛤？_来自小红书网页版.jpg",
        title: "物理老师打篮球一百个只进了三个",
        tx: "/image/sucai/c9ac2f1c-230d-4c80-ba00-401f6c66d92d.png",
        name: "蛤?",
        num: 10000,
        tran_num: "",
        islike: 1,
        tag: ["推荐", "穿搭", "职场"]
      },
      {
        id: 1001,
        cover: "/image/sucai/山东自考成绩_1_非酋_来自小红书网页版.jpg",
        title: "山东自考成绩",
        tx: "/image/sucai/e17c178b-bbc7-48c8-8063-74bcdd7146e9.png",
        name: "非酋?",
        num: 56,
        tran_num: "",
        islike: 0,
        tag: ["推荐", "穿搭", "男高"]
      },
      {
        id: 1002,
        cover: "/image/sucai/后悔没有早点来南昌😭真的太好吃了！！！_1_黑框（live纯享版）_来自小红书网页版.jpg",
        title: "后悔没有早点来南昌😭真的太好吃了！！！",
        tx: "/image/sucai/95d71d59-5a5f-4d61-b918-6837735e2332.png",
        name: "黑框",
        num: 2886,
        tran_num: "",
        islike: 0,
        tag: ["推荐", "美食"]
      },
      {
        id: 1003,
        cover: "/image/sucai/不懂就问，这车能跑过小米su7 Ultra吗？_1_山野睛子_来自小红书网页版.jpg",
        title: "不懂就问，这车能跑过小米su7 Ultra吗？",
        tx: "/image/sucai/2b747730-ccba-4759-b9b0-f1de69fd15f4.png",
        name: "山野睛子",
        num: 4087,
        tran_num: "",
        islike: 1,
        tag: ["推荐", "跑车", "职场"]
      }
    ]
  },
  onLoad(e) {
    this.countNum()
  },
  onShow(e) {
    this.countNum()
    let list = this.data.allNoramalData
    this.setData({
      noramalData: list
    })
  },
  getKey(e) {
    let key_word = e.currentTarget.dataset.key
    this.setData({
      key_word: key_word,
      flag: 1
    })
    //根据关键字对内容进行搜索
    this.enterSearch(key_word)
  },
  getSearch(e) {
    let key = e.detail.value
    this.setData({
      key_word: key,
      flag: 1
    })
    this.enterSearch(key)
  },
  clear() {
    this.setData({
      key_word: "",
      flag: 0
    })
    this.onShow()
  },
  toLike(e) {
    console.log(e)
    let id = e.currentTarget.dataset.id
    console.log(id)
    let list = this.data.noramalData
    list.forEach(element => {
      if (element.id == id) {
        if (element.islike == 1) {
          element.islike = 0
          element.num -= 1
        } else {
          element.islike = 1
          element.num += 1
        }
      }
      this.onShow()
    });
  },
  countNum() {
    let list = this.data.allNoramalData
    for (let i = 0; i < list.length; i++) {
      console.log(list[i].num)
      let num = list[i].num
      if (num >= 10000) {
        num = (num / 10000).toFixed(1) + "万"
        console.log(num)
      }
      list[i].tran_num = num
    }
    this.setData({
      allNoramalData: list
    })
  },
  enterSearch(key_word) {
    let allNoramalData = this.data.allNoramalData
    let list = []
    for (let index = 0; index < allNoramalData.length; index++) {
      let title = allNoramalData[index].title
      let name = allNoramalData[index].name
      let tag = allNoramalData[index].tag
      if (title.search(key_word) != -1 || name.search(key_word) != -1 || tag.indexOf(key_word) != -1) {
        list.push(allNoramalData[index])
      }
    }
    this.setData({
      noramalData: list
    })
  }
})